package com.repair.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.repair.entity.RepairmanLocation;
import com.repair.mapper.RepairmanLocationMapper;
import com.repair.service.RepairmanLocationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;

@Service
public class RepairmanLocationServiceImpl extends ServiceImpl<RepairmanLocationMapper, RepairmanLocation> implements RepairmanLocationService {

    @Autowired
    private RepairmanLocationMapper repairmanLocationMapper;

    @Override
    @Transactional
    public void updateLocation(Long repairmanId, Double latitude, Double longitude) {
        RepairmanLocation location = getByRepairmanId(repairmanId);
        if (location == null) {
            location = new RepairmanLocation();
            location.setRepairmanId(repairmanId);
            location.setStatus(0);
            location.setCreateTime(LocalDateTime.now());
            location.setUpdateTime(LocalDateTime.now());
        }

        location.setLatitude(latitude);
        location.setLongitude(longitude);
        location.setUpdateTime(LocalDateTime.now());
        location.setCreateTime(LocalDateTime.now());
        saveOrUpdate(location);
    }

    @Override
    public RepairmanLocation getByRepairmanId(Long repairmanId) {
        return repairmanLocationMapper.selectByRepairmanId(repairmanId);
    }

    @Override
    @Transactional
    public void updateStatus(Long repairmanId, Integer status) {
        RepairmanLocation location = getByRepairmanId(repairmanId);
        if (location != null) {
            location.setStatus(status);
            location.setUpdateTime(LocalDateTime.now());
            updateById(location);
        }
    }

    @Override
    public RepairmanLocation getRepairmanLocation(Long repairmanId) {
        return repairmanLocationMapper.selectByRepairmanId(repairmanId);
    }
} 